// pages/register/register.js
const app = getApp();
const md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    telephone: null, //普通入口
    verify_code: null,
    countdown: null,
    invitation_code: '',

    tel: null, //密码入口
    ver: null,

    password: null, //验证密码
    repet_password: null,

    token: null,

    inputOne: '',
    inputTwo: '',

  },
  //清除
  click_clear() {
    this.setData({
      telephone: '',
      flag_user: false,
    })
  },
  //获取协议
  geturl() {
    wx.request({
      url: app.globalData.url + 'api/sign/protocol',
      data: {

      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let url = e.data.data.url;
        this.setData({
          url: url,
        })
      },
    })
  },
  //去url页
  go_protocol() {
    wx.navigateTo({
      url: '../protocol/protocol?url=' + this.data.url + '&title=服务协议',
    })
  },
  //邀请码
  invitation_input(e) {
    let invitation_code = e.detail.value;
    this.setData({
      invitation_code: invitation_code,
    })
  },
  user_input: function(e) {
    // console.log(e.detail.value)
    let telephone = e.detail.value;
    this.setData({
      telephone: telephone,
    })
    if (telephone && this.data.verify_code) {
      this.setData({
        flag_user: true,
      })
    } else {
      this.setData({
        flag_user: false,
      })
    }

  },
  ver_input: function(e) {
    // console.log(e.detail.value)
    let verify_code = e.detail.value;
    this.setData({
      verify_code: e.detail.value,
    })
    if (verify_code && this.data.telephone) {
      this.setData({
        flag_user: true,
      })
    } else {
      this.setData({
        flag_user: false,
      })
    }

  },
  reg_inputOne: function(e) {
    var password = md5.hex_md5(e.detail.value),
      inputOne = e.detail.value;

    console.log('One1+' + e.detail.value)
    console.log('Two1+' + this.data.inputTwo)

    this.setData({
      password: password,
      inputOne: inputOne,
    })
    if (inputOne && this.data.inputTwo) {
      this.setData({
        flag_pass: true,
      })
    } else {
      this.setData({
        flag_pass: false,
      })
    }
  },
  reg_inputTwo: function(e) {
    var repet_password = md5.hex_md5(e.detail.value),
      inputTwo = e.detail.value;
    console.log('One2+' + e.detail.value)
    console.log('Two2+' + this.data.inputOne)
    // console.log('Two+' + e.detail.value)
    this.setData({
      repet_password: repet_password,
      inputTwo: inputTwo,
    })
    if (inputTwo && this.data.inputOne) {
      this.setData({
        flag_pass: true,
      })
    } else {
      this.setData({
        flag_pass: false,
      })
    }
  },
  getVer: function() {
    var that = this;
    //判断是否为已注册手机号
    // console.log('tel+' + this.data.telephone + ';ver+' + this.data.verify_code)
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/sign/telephone/count',
      data: {
        telephone: this.data.telephone
      },
      success: function(e) {
        // console.log(e)

        //手机号格式正确
        if (e.data.code == 0) {


          //手机号已经注册
          if (e.data.data.count == 1) {
            wx.showToast({
              title: '手机号已注册',
              icon: 'none'
            })
          } else {
            //手机号未注册
            that.setData({
              flag_focus: true,
            })
            //定义定时器开始时间
            that.setData({
              countdown: 60
            })
            //定时开始
            var timer = setInterval(function() {
              that.setData({
                countdown: that.data.countdown - 1
              })
              // console.log(that.data.countdown)
              if (that.data.countdown <= 0) {
                clearInterval(timer)
              }
            }, 1000)
            //开始请求
            wx.request({
              method: 'POST',
              url: app.globalData.url + 'api/sign/get/verify',
              data: {
                telephone: that.data.telephone,
                type: "register"
              },
              success: function(e) {
                console.log(e)
              }
            })
          }
        } else {
          //手机号格式错误
          wx.showToast({
            title: e.data.message,
            icon: 'none'
          })
        }

      }
    })

  },
  goLogin: function() {
    wx.redirectTo({
      url: '../login/login',
    })
  },
  //用户注册好，更新时间
  updateTime() {
    wx.request({
      method: "POST",
      url: this.globalData.url + 'api/user/update/logintime',
      data: {
        token: wx.getStorageSync('token'),
        from: "WxMiNi",
      },
      success: function(e) {
        //  console.log(e)
      }
    })
  },
  clickNext: function(e) {
    var that = this;

    if (this.data.tel) {
      //输入密码
      if (this.data.flag_pass) {
        let inputOne = this.data.inputOne;
        let inputTwo = this.data.inputTwo;
        // console.log("password的长度" + password)
        if (inputOne.length < 6 && inputTwo.length < 6) {
          //长度不够
          wx.showToast({
            title: '密码长度不小于6位',
            icon: 'none',
          })
        } else {
          //长度正常
          wx.request({
            method: "POST",
            url: app.globalData.url + 'api/sign/register',
            data: {
              telephone: this.data.tel,
              verify_code: this.data.ver,
              password: this.data.password,
              repet_password: this.data.repet_password,
              register_from: "WxMiNi",
              invitation_code: this.data.invitation_code,
            },
            success: function(e) {
              // console.log(e)

              if (e.data.code == 0) {
                //成功
                wx.redirectTo({
                  url: '../recruit/index/index'
                })

                // var timer = setTimeout(function () {
                //   wx.showModal({
                //     showCancel: false,
                //     content: '注册成功,请选择身份',
                //     confirmColor: "#ddba69",
                //     success: function (e) {
                //       console.log(e)
                //       wx.redirectTo({
                //         url: '../selectIdentity/selectIdentity?token=' + that.data.token,
                //       })
                //     }
                //   })
                //   clearTimeout(timer);
                // }, 200)

                that.setData({
                  token: e.data.data.token
                })
                console.log('token+' + that.data.token);
                app.globalData.token = that.data.token;
                wx.setStorageSync('token', that.data.token)
              } else {
                if (e.data.message == "验证码已失效！") {
                  wx.showModal({
                    showCancel: false,
                    content: '验证码已失效！',
                    confirmColor: "#ddba69",
                    success: function(e) {
                      // console.log(e)
                      wx.redirectTo({
                        url: '../register/register',
                      })
                    }
                  })
                } else if (e.data.message == "邀请码不正确，请检查") {
                  wx.showModal({
                    showCancel: false,
                    content: '邀请码不正确，请检查',
                    confirmColor: "#ddba69",
                    success: function(e) {
                      // console.log(e)
                      wx.redirectTo({
                        url: '../register/register',
                      })
                      that.setData({
                        invitation_code: '',
                      })
                    }
                  })
                } else {
                  wx.showToast({
                    title: e.data.message,
                    icon: 'none'
                  })
                }

              }
            }
          })
        }
      } else {
        return false;
      }


    } else {
      //普通入口
      console.log('tel+' + this.data.telephone + ';ver+' + this.data.verify_code)
      if (this.data.flag_user) {
        wx.request({
          method: 'POST',
          url: app.globalData.url + 'api/sign/verify/code',
          data: {
            telephone: this.data.telephone,
            verify_code: this.data.verify_code,
            type: "register",
          },
          success: function(e) {
            // console.log(e)
            if (e.data.code == 0) {
              wx.redirectTo({
                url: '../register/register?telephone=' + that.data.telephone + '&&verify_code=' + that.data.verify_code + '&&invitation_code=' + that.data.invitation_code,
              })
            } else {
              //手机号格式错误
              wx.showToast({
                title: e.data.message,
                icon: 'none'
              })
            }
          }
        })
      } else {
        return false;
      }

    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    this.geturl();
    if (!options.invitation_code) {
      var invitation_code = '';
    } else {
      var invitation_code = options.invitation_code;
    }
    this.setData({
      tel: options.telephone,
      ver: options.verify_code,
      invitation_code: invitation_code,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})